<?php

namespace app\api\controller;


use app\common\controller\Api;

use app\api\model\group\GroupCollect as GroupCollectModel;
use app\api\model\group\Group as GroupModel;

/**
 * 抱团收藏接口
 */
class Groupcollect extends Api
{

    protected $noNeedRight = ['*'];

    //收藏
    public function add()
    {
        $group_id = input('group_id',0);
        if (empty($group_id)) {
            $this->error('缺少参数抱团id');
        }
        $data = [
            'user_id' => $this->auth->id,
            'group_id' => $group_id,
        ];

        $collect = new GroupCollectModel();

        $info = GroupCollectModel::where(['group_id'=>$group_id, 'user_id' => $this->auth->id])->find();
        if (empty($info)){
            $res = $collect->save($data);
            if ($res){
                GroupModel::where(['group_id'=>$group_id])->setInc('collect_num',1);
            }
            $this->success('收藏成功',$res);
        } else {
            $this->error('收藏失败');
        }
    }

    //删除
    public function delete()
    {
        $group_id = input('group_id',0);
        if (empty($group_id)) {
            $this->error('缺少参数抱团id');
        }

        $info = GroupCollectModel::where(['group_id'=>$group_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)){
            $res = $info->delete();
            if ($res){
                GroupModel::where(['group_id'=>$group_id])->setDec('collect_num',1);
            }
            $this->success('取消成功',$res);
        } else {
            $this->error('取消失败');
        }
    }

    //是否收藏
    public function iscollect()
    {
        $group_id = input('group_id',0);
        if (empty($group_id)) {
            $this->error('缺少参数抱团id');
        }

        $info = GroupCollectModel::where(['group_id'=>$group_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)) {
            $this->success('',1);
        } else {
            $this->success('',0);
        }
    }

    //收藏列表
    public function lists()
    {
        $project = GroupCollectModel::alias('pc')
            ->join('group g','pc.group_id = g.group_id')
            ->where(['pc.user_id'=>$this->auth->id])
            ->field('pc.createtime,g.group_id,g.image,g.nickname,g.realname,g.title,g.name,g.people_num,
            g.profile,g.user_level,g.team_gender,g.taboo_id,g.activity_province,g.activity_city,g.activity_district')
            ->order('pc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$project);
    }

    //收藏排名
    public function ranking()
    {
        $lists = GroupModel::field('group_id,image,nickname,realname,title,name,people_num,profile,user_level
        team_gender,taboo_id,activity_province,activity_city,activity_district,collect_num')
            ->order('collect_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$lists);
    }


    //抱团的收藏列表
    public function collectList()
    {
        $group_id = input('group_id',0);

        $list = GroupCollectModel::alias('hc')
            ->join('user u','hc.user_id = u.id')
            ->where(['hc.user_id'=>$this->auth->id,'hc.group_id'=>$group_id])
            ->field('u.nickname,u.avatar,hc.createtime')
            ->order('hc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }
}